//
//  WDFirebaseManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/14.
//

#import "WDFirebaseManager.h"
#import "WDNSLog.h"
#import "WDCallBack.h"
@import FirebaseCore;
//@import FirebaseFirestore;
//@import FirebaseAuth;
//@import FirebaseAnalytics;
//@import FirebaseMessaging;


@interface WDFirebaseManager ()

@end

@implementation WDFirebaseManager

static WDFirebaseManager *_FirebaseManager = nil;
+ (instancetype)shareFirebaseManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _FirebaseManager = [[self alloc] init];
    });
    return _FirebaseManager;
}

- (BOOL)googlePlist
{
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info.plist" ofType:@""];
    if(path==NULL)
    {
        NSLog(@"GoogleService-Info不存在");
        return NO;
    }
    NSLog(@"GoogleService-Info存在");
    return YES;
}

- (void)firebaseRegisterForRemoteNotificationsApplication:(UIApplication *)application
{
    if (![self googlePlist])
    {
        return;
    }
    

    [FIRApp configure];
//    [FIRMessaging messaging].delegate = self;
    
}

- (void)firebaseApnsToken:(NSDate *)token
{
//    [FIRMessaging messaging].APNSToken = token;
}

- (void)firebaseRequestFCMToken
{
//    if (![self googlePlist])
//    {
//        return;
//    }
//    [[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) {
//      if (error != nil) {
//        WDLog(@"Error getting FCM registration token: %@", error);
//          if ([WDCallBack shareCallBack].onFCMTokenCallBack)
//          {
//              [WDCallBack shareCallBack].onFCMTokenCallBack(NO, @"", error.localizedDescription);
//          }
//      } else {
//        WDLog(@"FCM registration token: %@", token);
//        if ([WDCallBack shareCallBack].onFCMTokenCallBack)
//        {
//            [WDCallBack shareCallBack].onFCMTokenCallBack(YES, token, @"");
//        }
//      }
//    }];
}

//- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
//    WDLog(@"FCM registration token: %@", fcmToken);
//
//}

#pragma mark Ironsource Mediation
- (void)ISTaichiFIREvent:(ISImpressionData *)impressionData
{

//    NSDictionary *parameters = @{
//        kFIRParameterAdPlatform:@"ironSource",
//        kFIRParameterAdSource:impressionData.ad_network,
//        kFIRParameterAdFormat:impressionData.ad_unit,
//        kFIRParameterAdUnitName:impressionData.instance_name,
//        kFIRParameterCurrency:@"USD",
//        kFIRParameterValue:impressionData.revenue
//    };
//    WDLog(@"Ironsource Ad_Impression_Revenue:%@",parameters);
//    [FIRAnalytics logEventWithName:@"Ad_Impression_Revenue"
//                        parameters:parameters];
//    
//    float totalAdRevenue = [self getIronsourceTotalAdRevenue]+[impressionData.revenue floatValue];
//    WDLog(@"Ironsource 累计总广告收入:%lf",totalAdRevenue);
//    if (totalAdRevenue >= 0.01)
//    {
//        NSDictionary *totalParameters = @{
//            kFIRParameterAdPlatform:@"ironSource",
//            kFIRParameterAdSource:impressionData.ad_network,
//            kFIRParameterAdFormat:impressionData.ad_unit,
//            kFIRParameterAdUnitName:impressionData.instance_name,
//            kFIRParameterCurrency:@"USD",
//            kFIRParameterValue:[NSString stringWithFormat:@"%lf",totalAdRevenue]
//        };
//        WDLog(@"Ironsource Total_Ads_Revenue_001:%@",totalParameters);
//        [FIRAnalytics logEventWithName:@"Total_Ads_Revenue_001"
//                            parameters:totalParameters];
//        totalAdRevenue = 0.0;
//    }
//    [self saveIronsourceTotalAdRevenue:totalAdRevenue];
}

- (void)saveIronsourceTotalAdRevenue:(float)revenue
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%lf",revenue] forKey:@"IronsourceTotalAdRevenue"];
}

- (float)getIronsourceTotalAdRevenue
{
    NSString *total = [[NSUserDefaults standardUserDefaults] objectForKey:@"IronsourceTotalAdRevenue"];
    if (!total || total.length <=0)
    {
        return 0.0;
    }
    return [total floatValue];
}

#pragma mark AdMob Mediation
- (void)AdMobTaichiFIREvent:(GADAdValue *)value ad:(GADAppOpenAd *)ad loadedAdNetworkResponseInfo:(GADAdNetworkResponseInfo *)loadedAdNetworkResponseInfo
{
//    NSString *adRevenue = [NSString stringWithFormat:@"%@",value.value];
//    NSString *currencyCode = value.currencyCode;
//    NSString *adUnitId = loadedAdNetworkResponseInfo.adSourceInstanceID;
//    if (!adUnitId || adUnitId.length <= 0)
//    {
//        adUnitId = @"No ad_unit";
//    }
//    NSString *adSourceName = loadedAdNetworkResponseInfo.adSourceName;
//    if (!adSourceName || adSourceName.length <=0)
//    {
//        adSourceName = @"No ad_network";
//    }
//    NSString *adSourceInstanceName = loadedAdNetworkResponseInfo.adSourceInstanceName;
//    if (!adSourceInstanceName || adSourceInstanceName.length <= 0)
//    {
//        adSourceInstanceName = @"No instance_name";
//    }
//    NSDictionary *parameters = @{
//        kFIRParameterAdPlatform:@"AdMob",
//        kFIRParameterAdSource:adSourceName,
//        kFIRParameterAdFormat:adUnitId,
//        kFIRParameterAdUnitName:adSourceInstanceName,
//        kFIRParameterCurrency:currencyCode,
//        kFIRParameterValue:adRevenue
//    };
//    WDLog(@"AdMob Ad_Impression_Revenue:%@",parameters);
//    [FIRAnalytics logEventWithName:@"Ad_Impression_Revenue"
//                        parameters:parameters];
//    
//    float totalAdRevenue = [self getAdMobTotalAdRevenue]+[adRevenue floatValue];
//    WDLog(@"AdMob 累计总广告收入:%lf",totalAdRevenue);
//    if (totalAdRevenue >= 0.01)
//    {
//        NSDictionary *totalParameters = @{
//            kFIRParameterAdPlatform:@"AdMob",
//            kFIRParameterAdSource:adSourceName,
//            kFIRParameterAdFormat:adUnitId,
//            kFIRParameterAdUnitName:adSourceInstanceName,
//            kFIRParameterCurrency:currencyCode,
//            kFIRParameterValue:[NSString stringWithFormat:@"%lf",totalAdRevenue]
//        };
//        WDLog(@"AdMob Total_Ads_Revenue_001:%@",totalParameters);
//        [FIRAnalytics logEventWithName:@"Total_Ads_Revenue_001"
//                            parameters:totalParameters];
//        totalAdRevenue = 0.0;
//    }
//    [self saveAdMobTotalAdRevenue:totalAdRevenue];
}

- (void)saveAdMobTotalAdRevenue:(float)revenue
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%lf",revenue] forKey:@"AdMobTotalAdRevenue"];
}

- (float)getAdMobTotalAdRevenue
{
    NSString *total = [[NSUserDefaults standardUserDefaults] objectForKey:@"AdMobTotalAdRevenue"];
    if (!total || total.length <=0)
    {
        return 0.0;
    }
    return [total floatValue];
}

#pragma mark In-house Mediation
- (void)AdLTV_OneDayWithVaule:(NSString *)value
{
    
}

@end
